load detector

p =  struct('numofobject', 0, 'Appearance', [,], 'Position', [,], 'Scale', []);
node     = struct('frametime',     0,   ...
                  'detectorid',    0,   ... 
                  'location',      [],  ...
                  'height',        0,   ... 
                  'width',         0,   ... 
                  'velocity',      0,   ... 
                  'appearance',    [],  ... 
                  'scale',         0    ...
                  );
tracklets = struct('length',       0,  ...
                   'detectors',    {}  ...
                  );
framenode = struct('detectornum',  0,  ...
                   'detectors',    {}  ...
                   );
first = true;
show = false;
detectorid = 0;
tracklets_num = 0;
isintracklets = [];




for i = 930:1000
    
    
    indexi = i - 929;
    if show == true
        figure(i);
      %  figure(2 * indexi);
    end
    imagename = strcat('seq02-img-left/image_',  num2str(i,'%08d'), '_0.png');
    im = imread(imagename);
    mask = zeros(size(im,1), size(im,2), 'uint8');
    Loc= detector(i - 929).Location;
    objectnum = detector(i-929).numofdetectors;
    
    
    framenode(indexi).detectornum = detector(indexi).numofdetectors;
    
    for j=1:detector(indexi).numofdetectors
        % detetctorid  ++
        detectorid = detectorid + 1;
        
        im1 = rgb2gray(im(round(Loc(j,2)):round(Loc(j,4)),  round(Loc(j,1)):round(Loc(j,3)), :));
        
        if show == true
            set(0, 'CurrentFigure', i);
            subplot(1,objectnum,j);
            imshow(im1);
        end
        %set CurrentFigure
       
       % mask(Loc(j,2):Loc(j,4), Loc(j,1):Loc(j,3)) = 1;
        roi = [ round(Loc(j,2)); round(Loc(j,4)); round(Loc(j,1)) ; round(Loc(j,3))]; 
        
        framenode(indexi).detectors(j) = saveStructNode(i,            ...
                                                        detectorid,   ...
                                                        [Loc(j, 2),   ...
                                                        Loc(j, 1)],   ...
                                                        Loc(j,4) - Loc(j, 2),   ...
                                                        Loc(j,3) - Loc(j, 1),   ...
                                                        0,                      ...
                                                        anna_phog(im, 8, 360, 2, roi),    ...
                                                        sqrt((Loc(j,4) - Loc(j, 2)) * (Loc(j,3) - Loc(j, 1))) ...
                                                        );
%        p(indexi).Appearance(j,:) = anna_phog(im, 8, 360, 2, roi);
%        p(indexi).Position(j,1) = (Loc(j,2) + Loc(j,4))/2;
%        p(indexi).Position(j,2) = (Loc(j,1) + Loc(j,3))/2;
%        p(indexi).Scale(j) = sqrt((Loc(j,4) - Loc(j,2)) * (Loc(j,3) - Loc(j,1)));
        
        
        
        if show == true
         %   set(0, 'CurrentFigure', 2*indexi);
         %   subplot(1,objectnum, j);
         %   plot(framenode(indexi).detectors(j).appearance, '*'); %pause;
        end

        
      %  pause;
    end
    
    
    if ~first
        % hard connect
        %
        % d is affinity model
        d = affinitymodel(framenode(indexi - 1), framenode(indexi))
        
        [min_prematch, index_prematch] = min(d');
        [min_aftermatch, index_aftermatch] = min(d);
        index_prematch(find(min_prematch > 1000)) = -1;
        index_aftermatch(find(min_aftermatch > 1000)) = -1;
        
        for i = 1 : size(d, 1)
            if index_prematch(i) == -1
                continue;
            end
            if index_aftermatch(index_prematch(i)) ~=  i
                index_prematch(i) = -1;
            end
            if min_prematch(i) > 1000
                index_prematch(i) = -1;
            end
        end
        index_prematch
        match = find(index_prematch ~= -1);
        index_aftermatch(index_prematch(match)) = match;
        
        for i = 1:framenode(indexi).detectornum
            if index_aftermatch(i) == -1
                tracklets_num = tracklets_num + 1;
                tracklets(tracklets_num).length = 1;
                tracklets(tracklets_num).detectors(1) = framenode(indexi).detectors(i);
                isintracklets(framenode(indexi).detectors(i).detectorid) = tracklets_num;
            else
                pre_nodeid = framenode(indexi - 1).detectors(index_aftermatch(i)).detectorid;
                trackletsid = isintracklets(pre_nodeid);
                tracklets(trackletsid).length = tracklets(trackletsid).length + 1;
                tracklets(trackletsid).detectors(tracklets(trackletsid).length) = framenode(indexi).detectors(i);
                
                isintracklets(framenode(indexi).detectors(i).detectorid) = trackletsid;
            end
 
        end
        
    else
        for i = 1:framenode(indexi).detectornum
            tracklets_num = tracklets_num + 1;
            tracklets(tracklets_num).length = 1;
            tracklets(tracklets_num).detectors(1) = framenode(indexi).detectors(i);
            isintracklets(framenode(indexi).detectors(i).detectorid) = tracklets_num;
        end
    end
    first = false;
    disp(imagename);
    
    
    
   
end

save 'tracklets.mat' tracklets;
  

